<template>
  <div class="ts-wechat">
    <ts-header title="编辑微信" back></ts-header>
    <uploader title="微信收款二维码" :updTips="text"
              @loadImageUpload="loadImageUpload"
              @deleteImg="deleteImg"
              v-model="imageList"
              footTips="请上传您提现的微信的二维码图片。"></uploader>
    <div class="tf-save">
      <ts-button @click="savaWechat" :disabled="!imgFile" size="large" type="primary">保存</ts-button>
    </div>
  </div>
</template>

<script>
  import {ADD_WECHAT} from '../../../assets/js/api'

  export default {
    name: 'wechat',
    data() {
      return {
        imgFile: '',
        imageList: ['']
      }
    },
    created() {
      let {card} = this.$route.query;
      this.imgFile = card ? card : '';
      if (this.imgFile) {
        this.imageList.push(this.imgFile);
      }
    },
    methods: {
      async savaWechat() {
        let json = await this.$http.post(ADD_WECHAT, {imgFile: this.imgFile}, {show: true});
        if (json.result === 1) {
          let toast = this.$toast('添加成功');
          setTimeout(() => {
            toast.close();
            this.$router.push({name: 'meansManager'})
//            location.reload();
          }, 200)
        }
      },
      deleteImg(index) {
        this.imageList.splice(index, 1);
      },
      loadImageUpload() {
        if ($prod) {
          let onCameraSuccess = (data) => {
            this.imageList.length = 0;
            this.imgFile = data;
            this.imageList.push(data);
          };
          let onCameraError = (message) => {
          };
          navigator.camera.getPicture(onCameraSuccess, onCameraError, {
            //质量
            quality: 100,
            //不允许编辑
            allowEdit: false,
            //获取图片转为base64
            destinationType: Camera.DestinationType.DATA_URL,
            //编码类型JPEG
            encodingType: Camera.EncodingType.JPEG,
            //只允许图片
            mediaType: Camera.MediaType.PICTURE,
            //类型是选择图片
            sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
            targetWidth: 375
          });
        } else {
          alert('请在app内部使用');
        }
      }
    },
    computed: {
      text() {
        if (this.imgFile) {
          return '修改图片'
        } else if (this.imgFile === '') {
          return '上传图片'
        }
      }
    }
  }
</script>

<style lang="scss">
  @import "../../../assets/css/mixin";

  .ts-wechat {
    .tf-save {
      padding: 0 .36rem;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 1.10rem;
    }
  }
</style>
